package practices;

import java.util.Scanner;

public class Solution1 {
    /**
     * 单出一张
     * 出两张相同的牌(对子)
     * 出五张顺子(如12345)
     * 出三连对子(如112233)
     * 给10个数，表示1-10每种牌有几张，问最少要多少次能出完？
     *
     * 测试用例：
     * 1 1 1 2 2 2 2 2 1 1
     * <p>
     * 输出结果：3
     *
     * @param args
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int ans = 0;
        int num = 0;
        int temp;
        int[] cards = new int[10];
        for (int i = 0; i < 10; i++) {
            temp = sc.nextInt();
            cards[i] = temp;
            num += temp;
        }

        if (num == 1) {
            System.out.println(1);
            return;
        }

        ans = getNum(cards, 0);

        System.out.println(ans);
    }

    private static int getNum(int[] cards, int k) {
        int ans = Integer.MAX_VALUE;

        if (k >= cards.length) {
            return 0;
        }
        // 若牌数为0则继续下一张
        else if (cards[k] == 0) {
            return getNum(cards, k + 1);
        }

        // 优先出连对 因为能出的牌数最多
        if (k <= cards.length - 3 && cards[k] >= 2 && cards[k + 1] >= 2 && cards[k + 2] >= 2) {
            cards[k] -= 2;
            cards[k + 1] -= 2;
            cards[k + 2] -= 2;
            // 需要从当前位置开始出发
            ans = Math.min(1 + getNum(cards, k), ans);
            cards[k] += 2;
            cards[k + 1] += 2;
            cards[k + 2] += 2;
        }

        // 出顺子
        if (k <= cards.length - 5 && cards[k] >= 1 && cards[k + 1] >= 1 && cards[k + 2] >= 1 && cards[k + 3] >= 1 && cards[k + 4] >= 1) {
            cards[k] -= 1;
            cards[k + 1] -= 1;
            cards[k + 2] -= 1;
            cards[k + 3] -= 1;
            cards[k + 4] -= 1;
            // 需要从当前位置开始出发
            ans = Math.min(1 + getNum(cards, k), ans);
            cards[k] += 1;
            cards[k + 1] += 1;
            cards[k + 2] += 1;
            cards[k + 3] += 1;
            cards[k + 4] += 1;
        }

        // 出对子
        if (cards[k] >= 2) {
            cards[k] -= 2;
            ans = Math.min(1 + getNum(cards, k), ans);
            cards[k] += 2;
        }

        //出单子
        if (cards[k] >= 1) {
            cards[k] -= 1;
            ans = Math.min(1 + getNum(cards, k), ans);
            cards[k] += 1;
        }
        return ans;
    }

}